package com.rp.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.servlet.ModelAndView;

import com.rp.common.vo.PageParam;

/**
 * 所有自定义的控制器最好都继承自此类，以便获得一些常用的方法与变量。
 * 此类不依赖于任何的配置文件，我们的控制器只需要继承自此类，即可享有其中诸多方便快捷的方法与变量。
 * @author HuangJian
 *
 */
public abstract class RpController {
	
	protected final String SUCCESS = "success";
	protected final String ERROR = "error";
	protected final String ERROR_MSG = "操作失败，异常原因：";
	protected final String PROMPT = "prompt";
	
	protected final String WEBPAGE_TITLE = "webpageTitle";
	
	private final String DEFAULT_ENCODE = "UTF-8";
	
	protected HttpServletRequest request;
	protected HttpServletResponse response;
	protected HttpSession session;
	
	protected PageParam pageParam = null;
	
	protected static Logger log = Logger.getLogger(RpController.class);
	
	protected Map<String, Object> model = new HashMap<String, Object>();
	
	@ModelAttribute
	public void init(HttpServletRequest request, HttpServletResponse response){
		try {
			//设置请求与响应编码
			request.setCharacterEncoding(DEFAULT_ENCODE);
			response.setCharacterEncoding(DEFAULT_ENCODE);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		this.request = request;
		this.response = response;
		this.session = request.getSession();
		
		this.pageParam = new PageParam(request);
	}
	
	/**
	 * 返回一个请求参数的值
	 * @param name
	 * @return
	 */
	public String getPara(String name) {
		return request.getParameter(name);
	}
	
	/**
	 * 返回一个请求参数的值，如果该参数值不存在，则返回传入的默认值
	 * @param name
	 * @param defaultValue
	 * @return
	 */
	public String getPara(String name, String defaultValue) {
		String result = request.getParameter(name);
		return result != null && !"".equals(result) ? result : defaultValue;
	}
	
	/**
	 * 返回一个请求参数的值并转换为整数
	 * @param name
	 * @return
	 */
	public Integer getParaToInt(String name) {
		return toInt(request.getParameter(name), null);
	}
	
	/**
	 * 返回一个请求参数的值并转换为整数，如果该参数值不存在，则返回传入的默认值
	 * @param name
	 * @param defaultValue
	 * @return
	 */
	public Integer getParaToInt(String name, Integer defaultValue) {
		return toInt(request.getParameter(name), defaultValue);
	}
	
	/**
	 * 将参数值设置到请求中供视图调用显示
	 * @param name
	 * @param value
	 * @return
	 */
	public RpController setAttr(String name, Object value) {
		request.setAttribute(name, value);
		return this;
	}
	
	/**
	 * 删除请求中指定名称的参数值
	 * @param name
	 * @return
	 */
	public RpController removeAttr(String name) {
		request.removeAttribute(name);
		return this;
	}
	
	/**
	 * 给参数头尾添加百分号，用于模糊查询
	 * @param param
	 * @return
	 */
	public String fuzzyQueryParam(String param){
		if(param == null){
			return "%%";
		}
		param = "%" + param + "%";
		return param;
	}
	
	@Deprecated
	public ModelAndView getNewMav(){
		return new ModelAndView();
	}
	@Deprecated
	public ModelAndView getNewMav(String viewName){
		return new ModelAndView(viewName);
	}
	@Deprecated
	public ModelAndView getNewMav(String viewName, Map<String, Object> model){
		return new ModelAndView(viewName, model);
	}
	
	/**
	 * 转向，使用此方法跳转页面，如需添加参数返回给对应视图，可以通过model.put("key","value")方式添加
	 * @return 
	 */
	public ModelAndView forward(String viewName){
		return new ModelAndView(viewName, model);
	}
	
	/**
	 * 重定向，当提交表单保存或更新成功之后，应该调用此方法重定向到一个保存成功页面，这样可以防止重复提交表单
	 * @param viewName
	 * @return
	 */
	public ModelAndView redirect(String viewName){
		return new ModelAndView("redirect:"+viewName);
	}
	
	
	
	/**
	 * 获取项目在服务器中的目录位置，结尾不带"/"号
	 * @param request
	 * @return 
	 */
	public String getWebServerDirectory(){
		return this.request.getSession().getServletContext().getRealPath("");
	}
	
	
	private Integer toInt(String value, Integer defaultValue) {
		if (value == null || "".equals(value.trim()))
			return defaultValue;
		if (value.startsWith("N") || value.startsWith("n"))
			return -Integer.parseInt(value.substring(1));
		return Integer.parseInt(value);
	}
	
}
